<?php
namespace app\admin\controller;
use think\AjaxPage;
use think\Page;
use think\Db;

class Draw extends Base {
	public function index(){
		    //获取大转盘活动列表
		    $adminId = getAdminId();
		    $where = array();
		    if($adminId['suppliers_id']){
		        $where['t0.suppliers_id'] = $adminId['suppliers_id'];
		    }
		    $count =  M('draw t0')->where($where)->count();
			$Page = new Page($count,10);
		    $show = $Page->show();
		    $lists = M('draw t0')->field('t0.*,t1.appid')->join('tp_wx_user t1','t0.suppliers_id=t1.suppliers','left')->where($where)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		    $this->assign('lists',$lists);
		    $this->assign('pager',$Page);// 赋值分页输出
		    $this->assign('page',$show);// 赋值分页输出  
		return $this->fetch();
	}
	public function add_draw(){
		$adminId = getAdminId();
        $suppliers = M('suppliers')->field('suppliers_id,suppliers_name')->select();
        if($adminId['suppliers_id']){
            $suppliers_id = M('suppliers')->where('suppliers_id',$adminId['suppliers_id'])->find();
        }
        if(IS_POST){
        		$data = I('post.');
        	    $data['start_time'] = strtotime($data['start_time']);
        	    $data['end_time'] = strtotime($data['end_time']);
        	    $data['prize_level'] = json_encode($data['prize_level']);
        	    $data['prize_level_name'] = json_encode($data['prize_level_name']);
        	    $data['prize_name'] = json_encode($data['prize_name']);
        	    $data['prize_num'] = json_encode($data['prize_num']);
        	    $data['prob'] = json_encode($data['prob']);
        	    $data['suppliers_id'] = I('suppliers');
        	    $data['int_count'] = $data['int_count'];

    
        	    if(empty($data['id'])){
        	    	$data['create_time'] = time();
        	    	$row = M('draw')->add($data);
        	    }else{
        	    	$row =  M('draw')->where(array('id'=>$data['id']))->save($data);
        	    }
        	    if(!$row)
        	        $this->error('编辑大转盘活动失败');
        	    $this->success('编辑大转盘活动成功',U('Admin/Draw/index'));
        	    exit;
        }
        $id = I('get.id/d');
        if($id){
        	$draw = M('draw')->where(array('id'=>$id))->find();
        	$draw['prize_level']=json_decode($draw['prize_level'],true);
        	$draw['prize_level_name']=json_decode($draw['prize_level_name'],true);
        	$draw['prize_name']=json_decode($draw['prize_name'],true);
        	$draw['prize_num']=json_decode($draw['prize_num'],true);
        	$draw['prob']=json_decode($draw['prob'],true);
        	$this->assign('draw',$draw);
        }else{
        	$def['send_start_time'] = strtotime("+1 day");
        	$def['send_end_time'] = strtotime("+1 month");
        	$this->assign('draw',$def);
        }
        $this->assign('suppliers_id',$suppliers_id);
        $this->assign('suppliers',$suppliers);
		return $this->fetch();
	}
}